package com.central.apps.controller;

import com.central.app.common.constant.AppConstant;
import com.central.apps.service.ProjectCostService;
import com.central.bizcommon.constant.MessageConstant;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.exception.service.ServiceException;
import com.central.common.util.SysUserUtil;
import com.central.common.web.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 项目成本接口类
 *
 * @author 李大超
 * @date 2023/1/30
 */
@Slf4j
@RestController
@CrossOrigin
@Api(tags = "app的crud通用api")
@RequestMapping("/app/{app}")
public class ProjectCostController {
    @Autowired
    private ProjectCostService projectCostService;

    @PreAuthorize("hasAuthority('app:get/app/{app}/costTree')")
    @ApiOperation(value = "{app}树结构")
    @ApiImplicitParams({@ApiImplicitParam(name = "app", value = "应用名称", required = true, dataType = "String")})
    @PostMapping("/costTree")
    public Result costTree(@PathVariable String app, @RequestBody Map<String, Object> params) {
        List<Map<String, Object>> result = null;
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
        boolean isPermission = true;
        try {
            if (loginAppUser != null && AppConstant.USER_ADMIN.equals(loginAppUser.getUsername())) {
                isPermission = false;
                params.remove("deptcode");
            }
            if (app.startsWith("tb")) {
                isPermission = false;
            }
            result = projectCostService.costTree(app, params, isPermission);
        } catch (Exception e) {
            String message = MessageConstant.FAILED;
            if(e instanceof ServiceException){
                message = e.getMessage();
            }
            return Result.failed(message);
        }
        return Result.succeed(result, MessageConstant.SUCCEED);
    }

    @PreAuthorize("hasAuthority('app:get/app/{app}/costTree')")
    @ApiOperation(value = "{app}树结构")
    @ApiImplicitParams({@ApiImplicitParam(name = "app", value = "应用名称", required = true, dataType = "String")})
    @GetMapping("/getCostTree")
    public Result getCostTree(@PathVariable String app, @RequestParam Map<String, Object> params) {
        List<Map<String, Object>> result = null;
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
        boolean isPermission = true;
        try {
            if (loginAppUser != null && AppConstant.USER_ADMIN.equals(loginAppUser.getUsername())) {
                isPermission = false;
                params.remove("deptcode");
            }
            if (app.startsWith("tb")) {
                isPermission = false;
            }
            result = projectCostService.costTree(app, params, isPermission);
        } catch (Exception e) {
            String message = MessageConstant.FAILED;
            if(e instanceof ServiceException){
                message = e.getMessage();
            }
            return Result.failed(message);
        }
        return Result.succeed(result, MessageConstant.SUCCEED);
    }

    @PreAuthorize("hasAuthority('app:get/app/{app}/costStatisticTree')")
    @ApiOperation(value = "{app}树结构")
    @ApiImplicitParams({@ApiImplicitParam(name = "app", value = "应用名称", required = true, dataType = "String")})
    @GetMapping("/costStatisticTree")
    public Result costStatisticTree(@PathVariable String app, @RequestParam Map<String, Object> params) {
        List<Map<String, Object>> result = null;
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
        boolean isPermission = true;
        try {
            if (loginAppUser != null && AppConstant.USER_ADMIN.equals(loginAppUser.getUsername())) {
                isPermission = false;
                params.remove("deptcode");
            }
            if (app.startsWith("tb")) {
                isPermission = false;
            }
            result = projectCostService.costStatisticTree(app, params, isPermission);
        } catch (Exception e) {
            String message = MessageConstant.FAILED;
            if(e instanceof ServiceException){
                message = e.getMessage();
            }
            return Result.failed(message);
        }
        return Result.succeed(result, MessageConstant.SUCCEED);
    }
}
